package com.chenwut.studentManagerSystem.Tool;

import java.util.Scanner;

public class IdentityID {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入身份证");
            String ID = sc.next();

            boolean flag = checkID(ID);
            if (flag) {
                System.out.println("格式正确");
                break;
            } else {
                System.out.println("格式错误，请重新输入");
                continue;
            }
        }
    }

    private static boolean checkID(String id) {
        //必须18位
        if (id.length() != 18) {
            return false;
        }
        //不能以0开头
        if (id.startsWith("0")) {
            return false;
        }
        //前17位必须为数字
        for (int i = 0; i < id.length() - 1; i++) {
            char c = id.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                return false;
            }
        }
        //最后一位为数字或者X或x
        char endchar = id.charAt(id.length() - 1);
        if ((endchar >= '0' && endchar <= '9') || (endchar == 'X') || (endchar == 'x')) {
            return true;
        } else {
            return false;
        }
    }
}
